import * as proxy from '../proxy';
import ServiceBase from '../base';
import {PermissionsProxy, RoleProxy, RolesProxy} from '../proxies/auth-proxy';

const ACCOUNTS = '/accounts';
const ROLES = '/roles';

class RolesService extends ServiceBase {
    // 角色和权限相关
    getAccountRoles = (data) => {
        let promise = super.get({
            url: `${ACCOUNTS}/${data.accountId}${ROLES}`,
        });
        return proxy.httpResultProxy(promise, RolesProxy);
    };

    addAccountRole = (data) => {
        let promise = super.post({
            url: `${ACCOUNTS}/${data.accountId}${ROLES}`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise, RoleProxy);
    };

    setDefaultAccountRole = (data) => {
        let promise = super.put({
            url: `${ACCOUNTS}/${data.accountId}${ROLES}/${data.roleId}/default`,
        });
        return proxy.httpResultProxy(promise);
    };

    switchAccountRoleState = (data) => {
        let promise = super.put({
            url: `${ACCOUNTS}/${data.accountId}${ROLES}/${data.roleId}/state`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise);
    };

    deleteAccountRole = data => {
        let promise = super.delete({
            url: `${ACCOUNTS}/${data.accountId}${ROLES}/${data.roleId}`,
        });
        return proxy.httpResultProxy(promise);
    };

    getAccountRoleById = data => {
        let promise = super.get({
            url: `${ACCOUNTS}/${data.accountId}${ROLES}/${data.roleId}`,
            params: {
                filter: data.filter,
            },
        });
        return proxy.httpResultProxy(promise, RoleProxy);
    };

    updateAccountRole = data => {
        let promise = super.put({
            url: `${ACCOUNTS}/${data.accountId}${ROLES}/${data.roleId}`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise, RoleProxy);
    };

    getAddInPermissions = (data) => {
        let promise = super.get({
            url: `/accounts/${data.accountId}/permissions`,
        });
        return proxy.httpResultProxy(promise, PermissionsProxy);
    };

    getProjectRoles = (data) => {
        let promise = super.get({
            url: `/accounts/${data.accountId}/projects/${data.projectId}/roles`,
        });
        return proxy.httpResultProxy(promise, RolesProxy);
    };

    addProjectRole = (data) => {
        let promise = super.post({
            url: `${ACCOUNTS}/${data.accountId}/projects/${data.projectId}${ROLES}`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise, RoleProxy);
    };

    deleteProjectRole = (data) => {
        let promise = super.delete({
            url: `${ACCOUNTS}/${data.accountId}/projects/${data.projectId}${ROLES}/${data.roleId}`,
        });
        return proxy.httpResultProxy(promise);
    };

    getProjectRoleById = (data) => {
        let promise = super.get({
            url: `${ACCOUNTS}/${data.accountId}/projects/${data.projectId}${ROLES}/${data.roleId}`,
        });
        return proxy.httpResultProxy(promise, RoleProxy);
    };

    updateProjectRole = (data) => {
        let promise = super.put({
            url: `${ACCOUNTS}/${data.accountId}/projects/${data.projectId}${ROLES}/${data.roleId}`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise, RoleProxy);
    };

    switchProjectRoleState = (data) => {
        let promise = super.put({
            url: `${ACCOUNTS}/${data.accountId}/projects/${data.projectId}${ROLES}/${data.roleId}/state`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise);
    };
}

export default RolesService;
